/**
 * describe:
 *
 * @author chaP
 * @date 2019/03/28
 */
package CodingTest.AC20190328;

/**
 * 题目描述
 请实现两个函数，分别用来序列化和反序列化二叉树
 */

public class SerializeTree {
    class TreeNode{
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        TreeNode(int val){
            this.val = val;
        }
    }
    String Serialize(TreeNode root){
        StringBuffer sb = new StringBuffer();
        if(root == null){
            sb.append("#,");
            return sb.toString();
        }
        sb.append(root.val + ",");
        sb.append(Serialize(root.left));
        sb.append(Serialize(root.right));
        return sb.toString();
    }
    public int index = -1;
    TreeNode Deserialize(String str){
        index ++;
       TreeNode node = null;
        String[] str1 = str.split(",");
       if(!str1[index].equals("#")){
           node = new TreeNode(Integer.valueOf(str1[index]));
           node.left = Deserialize(str);
           node.right = Deserialize(str);
       }
       return node;

    }
}
